﻿// The dynamic form to show the detail of the object
Ext.define('App.desktop.DetailView', {
    extend: 'Ext.form.Panel',
    requires: [
            'Ext.form.*',
            'App.desktop.Workspace'
        ],
    alias: 'widget.detailView',
    frame: true,
    autoScroll: true,
    entity: null,
    operation: null,

    initComponent: function () {

        var me = this;
        me.items = me.createItems();

        me.callParent();

        var entity = me.child || me.entity; // If there is a child entity use it to build the form, otherwise use the entity
        App.desktop.Workspace.createOpeners(entity);
    },

    createItems: function () {

        var me = this;
        var items = [];

        var entity = me.child || me.entity; // If there is a child entity use it to build the form, otherwise use the entity

        Ext.each(entity.Attributes, function (a, i) {

            var type = App.desktop.Workspace.getType(a.Category);

            if (type == 'complex') {

                items.push({
                    xtype: 'box',
                    html: '<span style="float: left;">' + a.Name + ':</span><a href="javascript:void(0)" onclick="App.desktop.Workspace.openWindow(\'' + entity.FullName + '\',\'' + a.Name + '\',' + me.identifiers + ');"><img src="Content/Icons/application_form.png" class="icon-center"></a>'

                });
            }
            else if (type == 'collection') {

                items.push({

                    xtype: 'box',
                    html: '<span style="float: left;">' + a.Name + ':</span><a href="javascript:void(0)" onclick="App.desktop.Workspace.openWindow(\'' + entity.FullName + '\',\'' + a.Name + '\',' + me.identifiers + ');"><img src="Content/Icons/table.png" class="icon-center"></a>'

                });
            }
            else {

                items.push({
                    fieldLabel: a.Name,
                    name: a.Name,
                    xtype: me.getItemType(a.Type)
                });
            }

        }, this);

        return items;
    },

    enableCCFields: function (enable) {

        alert('enable: ' + enable);
    },

    getItemType: function (type) {

        return 'textfield';
    }

});